package com.soilflow.info.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.annotation.DataScope;
import com.soilflow.common.core.domain.entity.SysDept;
import com.soilflow.common.domain.LandAuctionDeposit;
import com.soilflow.common.domain.LandHire;
import com.soilflow.common.domain.LandInfo;
import com.soilflow.common.domain.LandRent;
import com.soilflow.common.domain.dto.LandHireDto;
import com.soilflow.common.domain.dto.statisticsDto;
import com.soilflow.common.domain.dto.villageDto;
import com.soilflow.common.domain.vo.LandHireVo;
import com.soilflow.common.utils.DateUtils;

import com.soilflow.info.mapper.LandAuctionDepositMapper;
import com.soilflow.info.mapper.LandHireMapper;
import com.soilflow.info.mapper.LandRentMapper;
import com.soilflow.info.service.ILandHireService;
import com.soilflow.info.service.LandContractService;
import com.soilflow.info.service.LandInfoService;
import com.soilflow.system.mapper.SysDeptMapper;
import com.soilflow.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author party
 * @date 2023-09-07
 */
@Service
public class LandHireServiceImpl extends ServiceImpl<LandHireMapper, LandHire> implements ILandHireService {
    @Autowired
    private LandHireMapper landHireMapper;
    @Autowired
    private LandRentMapper landRentMapper;
    @Autowired
    private LandInfoService landInfoService;
    @Autowired
    private ISysDeptService deptService;


    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public LandHireVo selectLandHireById(Long id)
    {
        return landHireMapper.selectLandHireById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param landHire 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    @DataScope(deptAlias = "land_hire")
    public List<LandHire> selectLandHireList(LandHireDto landHire) {
        return landHireMapper.selectLandHireList(landHire);
    }

    /**
     * 新增【租赁信息】，同时新增租金信息
     * 
     * @param landHire 【租赁】
     * @return 结果
     */
    @Override
    public boolean insertLandHire(LandHire landHire) {
        landHire.setCreateTime(DateUtils.getNowDate());
        //新增租赁信息
//        Long deptId = landHire.getDeptId();
//        SysDept sysDept = deptService.selectDeptById(deptId);
//        landHire.setDeptName(sysDept.getDeptName());
        LandInfo byId = landInfoService.getById(landHire.getLandId());
        landHire.setLandName(byId.getLandName());
        //新增租赁
        landHireMapper.insert(landHire);

        ////新增租金信息
        //if (insert>0) {
        //    LandRent landRent=new LandRent();
        //    landRent.setLandId(landHire.getLandId());
        //    landRent.setPrice(landHire.getPrice());
        //    landRent.setStartTime(landHire.getStartTime());
        //    landRent.setEndTime(landHire.getEndTime());
        //    landRent.setLandName(byId.getLandName());
        //    landRentMapper.insert(landRent);
        //}
        return true;
    }

    /**
     * 修改【租赁信息】，修改租赁信息后，同时新增租金信息
     * 
     * @param landHire 【请填写功能名称】
     * @return 结果
     */
    @Override
    public boolean updateLandHire(LandHire landHire) {
//        return landHireMapper.updateLandHire(landHire);
        //修改租赁信息
        int i = landHireMapper.updateById(landHire);
        if (i>0) {
            LandRent landRent = new LandRent();
            landRent.setLandId(landHire.getLandId());
            landRent.setPrice(landHire.getPrice());
//            landRent.setDeptId(landHire.getDeptId());
//            SysDept sysDept = deptService.selectDeptById(landHire.getDeptId());
//            landRent.setDeptName(sysDept.getDeptName());
            LandInfo byId = landInfoService.getById(landHire.getLandId());
            landRent.setLandName(byId.getLandName());
            landRent.setStartTime(landHire.getStartTime());
            //新增
            //新增之前需要查看修改租金是否有相同，有则不能新增
            BigDecimal fund = landRentMapper.selectRecently(landHire.getLandId());
            System.out.println("fund======"+fund);
            if (landHire.getPrice().compareTo(fund)!=0) {
                landRentMapper.insert(landRent);
            }
            return true;
        }else {
            return false;
        }
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteLandHireByIds(Long[] ids)
    {
        for (Long id : ids) {
            LambdaUpdateWrapper<LandHire> wrapper=new LambdaUpdateWrapper<>();
            wrapper.eq(LandHire::getId,id);
            wrapper.set(LandHire::getDeleted,1);
            int update = landHireMapper.update(null, wrapper);
        }
        return 1;
//        return landHireMapper.deleteLandHireByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteLandHireById(Long id)
    {
        LambdaUpdateWrapper<LandHire> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(LandHire::getId,id);
        wrapper.set(LandHire::getDeleted,1);
        return landHireMapper.update(null, wrapper);
//        return landHireMapper.deleteLandHireById(id);
    }

    /**
     * 按照年份统计租赁数据
     * @return
     */
    @Override
    public List<statisticsDto> statistics() {
        List<statisticsDto> statistics = landHireMapper.statistics();
        return statistics;
    }

    @Override
    public List<villageDto> village() {
        List<villageDto> villages = landHireMapper.village();
        return villages;
    }

    /**
     * 统计今年租赁土地数量
     * @return
     */
    @Override
    public int thisyear() {
        int total = landHireMapper.thisyear();
        return total;
    }

}
